/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itsc.ds2.bl.DiscBlLocal;
import mx.itsc.ds2.bl.SaleDetailBlLocal;
import mx.itsc.ds2.model.Discs;
import mx.itsc.ds2.model.SaleDetails;

/**
 *
 * @author Eli
 */
@ManagedBean
@SessionScoped
public class SaleDetailsBean {

    @EJB
    private DiscBlLocal discBl;
    @EJB
    private SaleDetailBlLocal saleDetailBl;
    /**
     * Creates a new instance of SaleDetailsBean
     */
    private List<SaleDetails> listSaleDetail;
    private SaleDetails saleDetail;
    private List<Discs> selectedDiscs;
    private Map<String, String> disc;
    Discs discs = new Discs();
    private String accion;
    private int selectedDisc;

    public SaleDetailsBean() {
        
        disc = new HashMap<String, String>();
        disc.put(discs.getArtist(), discs.getArtist());
    }

    public List<SaleDetails> getListSaleDetail() {
        listSaleDetail=saleDetailBl.getAll();
        return listSaleDetail;
    }

    public SaleDetails getSaleDetail() {
        return saleDetail;
    }

    public void setSaleDetail(SaleDetails saleDetail) {
        this.saleDetail = saleDetail;
    }

    public String procesarSeleccion() {
        selectedDiscs = new ArrayList<Discs>();
        saleDetail.setIdDisc(discBl.findById(selectedDisc));
        return "ConfirmSaleDetails";
    }

    public String processAction() {
        if(accion.equalsIgnoreCase("Nuevo")){
        saleDetail.setIdDisc(discBl.findById(selectedDisc));
        }
        return "ConfirmSaleDetails";
    }

    public String procesarAction() {
        saleDetailBl.register(saleDetail);
        accion = "";
        return "SaleDetailsList";
    }

    //listeners-----------------------------------------------------------------------
    public void prepararNuevo(ActionEvent e) {
        listSaleDetail = new ArrayList<SaleDetails>();
        accion = "Nuevo";
        saleDetail = new SaleDetails();
    }

    public void agregarDisco(ActionEvent evt) {
        if (!selectedDiscs.contains(discBl.findById(selectedDisc))) {
            selectedDiscs.add(discBl.findById(selectedDisc));
        } else {
        }
    }
}
